package com.whl.sm.service.impl.expense;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.expense.ContractTenancyMapper;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.expense.ContractTenancy;
import com.whl.sm.model.expense.ExpenseType;
import com.whl.sm.model.expense.TenancyBill;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.expense.ContractTenancyService;
import com.whl.sm.service.expense.ExpenseTypeService;
import com.whl.sm.service.expense.TenancyBillService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-25
 */
@Service
@Slf4j
public class ContractTenancyServiceImpl extends ServiceImpl<ContractTenancyMapper, ContractTenancy> implements ContractTenancyService {


    @Autowired
    @Lazy
    private TenancyBillService tenancyBillService;

    @Autowired
    @Lazy
    private AssetsTenantService assetsTenantService;

    @Lazy
    @Autowired
    private ComUnitsService comUnitsService;


    @Autowired
    private ExpenseTypeService expenseTypeService;

    @Override
    public Result update(ContractTenancy insertContract) {
        try{
            if (insertContract.getId() == null){
                getBaseMapper().insert(insertContract);
            }else {
                getBaseMapper().updateById(insertContract);
            }
            return Result.ok();
        }catch (Exception e){
            return Result.fail();
        }
    }

    @Override
    public Result addBill(List<String> idList) {
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
            for(String id :idList){
                ContractTenancy target = getById(id);
                Date lastMonth = getLastMonth(new Date());
                String monthSrc = sf.format(lastMonth);
                int count = tenancyBillService.count(new QueryWrapper<TenancyBill>()
                        .eq("contract_id",id)
                        .eq("bill_month",monthSrc)
                );
                if (count > 0) {
                    return Result.build(444,"账单已存在！");
                }else {
                    AssetsTenant relateTenant = assetsTenantService.getById(target.getTenantId());
                    ComUnits assets = comUnitsService.getById(relateTenant.getAssetsId());
                    ExpenseType expenseType = expenseTypeService.getById(target.getExpenseId());
                    double price = assets.getUnitArea()*expenseType.getUnitPrice()+expenseType.getDefaultPrice();
                    TenancyBill insert = new TenancyBill();
                    insert.setContractId(id);
                    insert.setUnitId(relateTenant.getAssetsId());
                    insert.setBillMonth(monthSrc);
                    insert.setBillPrice(price);
                    Result insertResult =  tenancyBillService.update(insert);
                    if (insertResult.getCode() != 200){
                        return insertResult;
                    }
                }
            }
        }catch (Exception e){
            log.error("收费管理---->租赁合同:生成合同当月账单失败！");
            return Result.fail();
        }
        return Result.ok();
    }

    private Date getLastMonth(Date nowDate){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH,-1);
        return c.getTime();
    }
}
